package com.help.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.help.domain.pojo.HelpCategory;
import com.help.domain.pojo.HelpItem;
import com.help.domain.vo.HelpCenterVo;
import com.help.enums.HelpNodeType;
import com.help.mapper.HelpItemMapper;
import com.help.service.IHelpCategoryService;
import com.help.service.IHelpItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.help.service.IHelpTagService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 二级分类与内容表 服务实现类
 * </p>
 *
 * @author 小杰大人
 * @since 2025-10-08
 */
@Service
public class HelpItemServiceImpl extends ServiceImpl<HelpItemMapper, HelpItem> implements IHelpItemService {
    @Resource
    IHelpCategoryService iHelpCategoryService;

    @Resource
    IHelpTagService iHelpTagService;
    @Override
    public List<HelpCategory> getHelpCategorys() {
        return iHelpCategoryService.list(new LambdaQueryWrapper<HelpCategory>().orderByDesc(HelpCategory::getIsDefault).orderByAsc(HelpCategory::getSortOrder));
    }

    @Override
    public List<HelpCenterVo> getHelpCenter(Integer categoryId) {
        List<HelpItem> helpItems = baseMapper.getHelpCenter(categoryId);
        // 存储二级分类 根据 sortOrder 进行排序
        // 由于 sortOrder 是 int 并且递增，可直接利用数组结构进行有序存储
        // SQL可以确保二级分类是按照 sortOrder 有序返回
        List<HelpCenterVo> centerVoList = new ArrayList<>();
        // 哈希表 用于构建 父子结构关系
        Map<Long,HelpCenterVo> map = new HashMap<>();
        helpItems.stream().filter(helpItem -> helpItem.getParentId() == null).forEach(helpItem -> {

            HelpCenterVo helpCenterVo = new HelpCenterVo(helpItem.getId(),HelpNodeType.SUBCATEGORY, helpItem.getTitle(), null,
                    new TreeSet<>(Comparator.comparingInt(HelpCenterVo::getSortOrder)), helpItem.getSortOrder(),null,null);
            centerVoList.add(helpCenterVo);
            map.put(helpItem.getId(), helpCenterVo);
        });
        for (HelpItem helpItem : helpItems) {
            // 过滤非叶子节点
            if (helpItem.getParentId() == null || !Objects.equals(helpItem.getItemType(), HelpNodeType.CONTENT.getValue())){
                continue;
            }
            // 得到父节点
            HelpCenterVo parent = map.get(helpItem.getParentId());
            // 构建子节点
            List<String> tagNamesByIds = iHelpTagService.getHelpTagNamesByIds(helpItem.getTagIds());
            HelpCenterVo child = new HelpCenterVo(helpItem.getId(),HelpNodeType.CONTENT, helpItem.getTitle(), helpItem.getContent() , null,
                    helpItem.getSortOrder(),tagNamesByIds,helpItem.getUpdateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            parent.getChildren().add(child);
        }
        return centerVoList;
    }
}
